//index.js
//获取应用实例
const globalData = getApp().globalData;
const request = require('../../utils/request');
const { watch, computed } = require('../../utils/computed');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

var num = 8,type = 'distance';

function loadingData(that,page,num,rid){
    if(that.data.upflag){
        if(rid == undefined){
            rid = ''
        }
        request.HttpRequst(true,'reserve',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":page,"num": num,"type": type,'type_rid':rid},"GET",false,res => {
            //console.log(res.data)
            if(res.data != ''){
                let goodsData = that.data.goods.concat(res.data)
                that.setData({
                    goods: goodsData
                })
            }else{
                that.setData({
                    upflag: false
                })
            }
        })
    }  
}

Page({
    data: {
        imgUrls: [
            'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
            'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
            'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
        ],
        list:[0,1,2,3,4],
        indicatorDots: true,
        autoplay: true,
        circular: true,
        interval: 3000,
        duration: 500,
        location: '',
        longitude: wx.getStorageSync('longitude'),
        latitude: wx.getStorageSync('latitude'),
        goods: [1,2,3,4,5],   //首页商家数据
        recommedGoods: [],
        upflag: true,
        page: 1,
        menuIndex:1,
        imgUrl:request.imgUrl,
        banner: [1,1,1],  //首页轮播图
        middle: [],  //中间图片,
        array: ['粤菜','川菜','鲁菜'],  //菜式分类
        index:0,
        rid: '', //菜式分类标志
        isCallWaiter: globalData.isCallWaiter,   //是否呼叫服务员
        isShowOrder: false,  //是否显示存在订单
        orderId: 0
    },
    onLoad: function(e) {
        try{
            if(e.code){
                wx.setStorageSync('code',e.code)
            }
        } catch(e){
            console.log('code不存在')
        }
        
        this.setData({
            page:1
        })
        //获取banner及图片
        request.HttpRequst(true,'program/banner',false,'',{"id":1},"GET",false,res => {
            console.log(res)
            this.setData({
                banner: res.data.banner,
                middle: res.data.middle
            })
        })
        //获取菜式分类
        request.HttpRequst(true,'typeReserveData',false,'','',"GET",false,res => {
            console.log('菜式分类：',res);
            this.setData({
                array: res.data
            })
        })


        if(this.data.location == ''){
            //获取地理位置
            var qqMap = new QQMapWX({
                key: 'MFJBZ-WFGW6-7WDSW-ETQYY-FZP3S-MSFQ5' // 必填
            });
            var that = this;
            wx.getLocation({
            type: 'gcj02',
            success: function(res) {
                    let latitude = res.latitude
                    let longitude = res.longitude
                    wx.setStorageSync('latitude', latitude)
                    wx.setStorageSync('longitude', longitude)
                    console.log("维度："+latitude+"经度："+longitude);
                    console.log(res);
                    qqMap.reverseGeocoder({
                        location: {
                            latitude: latitude,
                            longitude: longitude
                        },
                        success: function(res) {
                            console.log(res),
                            wx.setStorageSync('address', res.result.address);
                            wx.setStorageSync('location', res.result.address_component.street_number);
                            that.setData({
                                location: wx.getStorageSync('location'),
                                longitude: wx.getStorageSync('longitude'),
                                latitude: wx.getStorageSync('latitude'),
                            })
                            request.HttpRequst(true,'program/indexRecommend',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":that.data.page,"num": num},"GET",false,res => {
                                console.log(res.data)
                                that.setData({
                                    recommedGoods: res.data
                                })
                            })
                            request.HttpRequst(true,'reserve',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":that.data.page,"num": num,"type": type},"GET",false,res => {
                                
                                that.setData({
                                    goods: res.data
                                })
                            })
                        },
                        fail: function(res) {
                            
                        },
                        complete: function(res) {
                            console.log(res);
                        }
                    });
                },
                fail: function() {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        content: '获取您的位置失败，可能导致部分功能无法使用,后期还使用小程序，需在微信【发现】--【小程序】--删除【掌里通餐饮系统】，重新搜索授权登陆，方可使用',
                        success: function(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                }
            })
        }else{
            request.HttpRequst(true,'program/indexRecommend',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num},"GET",false,res => {
                console.log(res.data)
                this.setData({
                    recommedGoods: res.data
                })
            })
            request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type},"GET",false,res => {
                console.log(res.data)
                console.log(this.data.imgUrl)
                this.setData({
                    goods: res.data
                })
            })
        }

        //页面转发
        wx.showShareMenu({
            withShareTicket: true
        })
    },
    onShow() {
        this.setData({
            isCallWaiter: globalData.isCallWaiter   //是否呼叫服务员
        })
        request.HttpRequst(true,'indexservice',false,'',{'token':wx.getStorageSync('sessionId')},"post",false,res => {
            console.log(res)
            if(res.code == 200) {
                this.setData({
                    isShowOrder: true,
                    orderId: res.data
                })
                
            }else {
                this.setData({
                    isShowOrder: false
                })
            }
        })
    },

    //打开地理位置
    goLocation(){
        var that = this
        wx.chooseLocation({
            success: function(e){
                console.log(e)
                wx.setStorageSync('latitude', e.latitude)
                wx.setStorageSync('longitude', e.longitude)
                that.setData({
                    location: e.name,
                    longitude: e.longitude,
                    latitude: e.latitude,
                    upflag: true,
                    page: 1
                })
                //重新获取数据
                request.HttpRequst(true,'program/indexRecommend',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":that.data.page,"num": num},"GET",false,res => {
                    console.log(res.data)
                    that.setData({
                        recommedGoods: res.data
                    })
                })
                request.HttpRequst(true,'reserve',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":that.data.page,"num": num,"type": type},"GET",false,res => {
                    that.setData({
                        goods: res.data
                    })
                })
            },
            fail: function(e){
                console.log("选择地址失败")
            }
        })
    },
    goParcel(){
        wx.navigateTo({
            url: '../../pagesB/activity/share/share'
        })
    },
    goLine(){
        wx.navigateTo({
            url: 'list/line/line'
        })
    },
    goAdvance(){
        wx.navigateTo({
            url: 'list/advance/advance'
        })
    },
    goActivity(){
        wx.navigateTo({
            url: 'list/activity/activity'
        })
    },
    goRecommed(e){
        var name = e.currentTarget.dataset.name
        wx.navigateTo({
            url: 'discount/discount?name='+name
        })
    },
    goDiscount(e){
        var name = e.currentTarget.dataset.name
        wx.navigateTo({
            url: 'discount/discount?name='+name
        })
    },
    goShop(e){
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        console.log(type)
        if(type == 'reserve'){
            wx.navigateTo({
                url: 'list/advance/shop/shop?id='+id
            })
        }else{
            wx.navigateTo({
                url: 'list/advance/shop/shop?id='+id
            })
        }
        
    },
    /*下拉获取更多电影数据*/
    onReachBottom:function(){
        var that = this;
        that.data.page++;
        loadingData(that,that.data.page,num,that.data.rid);
    },

    //下拉刷新
    onPullDownRefresh() {
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.onLoad();
        this.setData({
            upflag: true,
            page:1
        })
        wx.hideNavigationBarLoading(); 
        wx.stopPullDownRefresh();  
    },


    //附近数据处理
    // getDistance () {
    //     this.setData({
    //         page:1,
    //         menuIndex:1
    //     })
    //     type = 'distance'
    //     request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type},"GET",false,res => {                 
    //         this.setData({
    //             goods: res.data,
    //             upflag: true
    //         })
    //     })
    // },
    //销量数据处理
    getSales () {
        this.setData({
            page:1,
            menuIndex:2,
            rid: ''
        })
        type = 'sales'
        request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true
            })
        })
    },
    //评分数据处理
    getScore () {
        this.setData({
            page:1,
            menuIndex:3,
            rid: ''
        })
        type = 'score'
        request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true
            })
        })
    },
    goSearch() {
        wx.navigateTo({
            url: 'search/search'
        })
    },

    //跳到预定
    goAdvanceShop(e) {
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: 'list/advance/shop/shop?id='+id
        })
    },

    //跳到排队
    goLineShop(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: 'list/line/lineShop/lineShop?id='+id
        })
    },

    bindPickerChange(e) {
        console.log(e)
        type = 'distance';
        let rid = this.data.array[e.detail.value].id;
        if(e.detail.value == 0){
            rid = '';
        }
        this.setData({
            index: e.detail.value,
            page:1,
            menuIndex:1,
            rid: rid
        })
        request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type,'type_rid':rid},"GET",false,res => {
            console.log(res)                 
            this.setData({
                goods: res.data,
                upflag: true
            })
        })
    },

    //查看存在的订单
    seeOrders() {
        wx.navigateTo({
            url: '/pages/my/order/detail/detail?id='+this.data.orderId+'&order_type=1'
        })
    },

    //点击banner图链接
    clickBanner(e) {
        let i = e.currentTarget.dataset.index
        if(i == 1) {
            wx.navigateTo({
                url: '../../pagesB/activity/followGzh/followGzh'
            })
        }
        if(i == 2) {
            wx.navigateTo({
                url: '../../pagesB/activity/share/share'
            })
        }
    }
})